﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ProcessHandles;
using UnityEngine.SceneManagement;

public class Controller : MonoBehaviour
{
    public Image ImageDisplay;
    public Text CountDownNubmerText;
    public Text PauseText;
    public Text tipText;

    public GameObject UI_Panel;
    public EyeTracker eyeTracker;

    private bool isPause = false;
    private int currentStimulusIdx;
    private Coroutine coroutineProcessing;
    private Coroutine coroutineOneSection;
    private Coroutine coroutineCountDown;

    public void Init()
    {
        Serializer.GazePoints.Clear();
        Serializer.ImageIdxOfGazePoints.Clear();

        /*------------------//眼动仪链接检测------------*/
        if (!Tobii.Gaming.Examples.Calibration.CalibrationHelper.Connected)
        {
            tipText.text = "眼动仪未连接";
            tipText.gameObject.SetActive(true);

            this.enabled = false;
            return;
        }

        UI_Panel.SetActive(false);
        ImageDisplay.preserveAspect = true;
        CountDownNubmerText.gameObject.SetActive(true);

        coroutineCountDown = StartCoroutine(CountDownTip());
    }

    void Update()
    {
        if (!isPause && Input.GetKeyDown(KeyCode.R))
        {
            StopAllCoroutines();
            ImageDisplay.gameObject.SetActive(false);
            CountDownNubmerText.gameObject.SetActive(false);

            Init();
        }

        if (!isPause && Input.GetKeyDown(KeyCode.Escape))
        {
            StopAllCoroutines();
            EndProcess(true);
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            isPause = !isPause;
            Time.timeScale = isPause ? 0f : 1f;
            PauseText.gameObject.SetActive(isPause);
        }

    }

    void EndProcess(bool esc = false)
    {
        if (!esc)
        {
            DataIO.handlingPath = "";

            Serializer.Serialize();

            SceneManager.LoadScene(1);
        }

        UI_Panel.SetActive(true);

        ImageDisplay.gameObject.SetActive(false);

        CountDownNubmerText.gameObject.SetActive(false);

        this.enabled = false;



        /*----------------------------------------------------*/

        //DataIO.AddParticipantIntoTxt();

        //eyeTracker.DrawPointCloud();

        //eyeTracker.PutToText();

        //eyeTracker.Visualize();
    }

    IEnumerator Processing()
    {
        int n = ProcessHandles.Cfg_Info.s_StimulusList.Count;
        for (int i = 0; i < n; i++)
        {
            currentStimulusIdx = i;

            Debug.LogError(Cfg_Info.s_StimulusIdxList[i]);

            ImageDisplay.gameObject.SetActive(true);
            ImageDisplay.sprite = ProcessHandles.Cfg_Info.s_StimulusList[i];


            coroutineOneSection = StartCoroutine(OneSection());

            yield return new WaitForSeconds(
                ProcessHandles.Cfg_Info.s_ImageDuration + ProcessHandles.Cfg_Info.s_IntvDuration);

            while (ImageDisplay.gameObject.activeSelf) yield return null; 
        }
        EndProcess();
    }

    IEnumerator CountDownTip()
    {

        for (int i = Cfg_Info.s_CountDown; i > 0; i--)
        {
            CountDownNubmerText.text = i.ToString();
            yield return new WaitForSeconds(1);
        }

        CountDownNubmerText.gameObject.SetActive(false);

        coroutineProcessing = StartCoroutine(Processing());
    }

    IEnumerator OneSection() 
    {
        float tick = 1.0f / Cfg_Info.s_Frequency;
        for (float i = 0; i < Cfg_Info.s_ImageDuration; i += tick)
        {
            eyeTracker.AppendGazePoint();

            Serializer.ImageIdxOfGazePoints.Add(Cfg_Info.s_StimulusIdxList[currentStimulusIdx]);

            yield return new WaitForSeconds(tick);
            //CountDownNubmerText.text = i.ToString();
        }
        ImageDisplay.gameObject.SetActive(false);
    }
}
